#region File Description
//  XAEnima 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: This Screen is used to choose users frequencies
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XAEnima.Core.GameManager;
using XAEnima.Core.Stimulation.Tipi;
using XAEnima.Core.Stimulation.External;
using XAEnima.gui;
using PCClient_DLL_1_0;
using SharedContent;
#endregion



namespace XAEnima.Core.Screens.Domotica
{
    public delegate void PlayAction();
    public class Domotica : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D env ,bg ,bg_dark ,bg_light;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        bool OldState = false;
        float screenscale;
        Rectangle area;
        Matrix SpriteScale;
        Dictionary<string, int> HIM_RESPONSES = new Dictionary<string, int> { { "UP", 16 }, { "DOWN", 32 }, { "LEFT", 1 }, { "RIGHT", 8 } };
        IInputManager InputManagerService;
        StimulationManager sm;
        IComunicationManager sender;
        List<Vector2> pos = new List<Vector2>();
        List<String> ambienti = new List<string>();
        List<UIlabel> ambienti_label = new List<UIlabel>();
        int ambientIndex = 0;
        public event PlayAction OnBack;
        public event PlayAction OnEnvironmentSelect;
        Boolean isFirstime = true;
        float time = 0;

        public Domotica(Game game)
            : base(game)
        {
            
            InputManagerService = (IInputManager)Game.Services.GetService(typeof(IInputManager));
            InputManagerService.EnterPressed += new PressedEventHandler(EnterIsPressed);
            InputManagerService.EnterReleased += new PressedEventHandler(EnterIsReleased);
            InputManagerService.LeftPressed += new PressedEventHandler(LeftIsPressed);
            InputManagerService.RightPressed += new PressedEventHandler(RightIsPressed);
            InputManagerService.XPressed += new PressedEventHandler(XIsPressed);
            InputManagerService.EscapeReleased += new PressedEventHandler(XIsPressed);
            sm = new StimulationManager(Game);

            
        }


        public override void Initialize()
        {
            screenscale = (float)(Game.GraphicsDevice.Viewport.Width / 1600f);
            spriteBatch = (Game.Services.GetService(typeof(SharedContext)) as SharedContext).sprite_batch;
            sender = (IComunicationManager)Game.Services.GetService(typeof(IComunicationManager));
            area = Game.GraphicsDevice.Viewport.TitleSafeArea;
            float x_pos = (float)Game.GraphicsDevice.Viewport.Width / 2.82f;
            float y_pos = (float)Game.GraphicsDevice.Viewport.Height / 3;
            env = Game.Content.Load<Texture2D>(@"Screens/Domotica/ambiente");
            bg = Game.Content.Load<Texture2D>(@"Screens/Domotica/bg");
            bg_dark = Game.Content.Load<Texture2D>(@"Screens/Domotica/bg_dark");
            bg_light = Game.Content.Load<Texture2D>(@"Screens/Domotica/bg");
            ambienti_label.Add(new UIlabel("Ambiente Uno", "Ambiente Uno", "Arial", Game));
            ambienti_label.Add(new UIlabel("Ambiente Due", "Ambiente Due", "Arial", Game));
            pos.Add(new Vector2(370 * screenscale, 340 * screenscale));
            pos.Add(new Vector2(870 * screenscale, 340 * screenscale));
            sender.HIMRequestParams();
            sm.Init();
            sm.SetFrequencies(16,24,32,48);
          /*  Light light = new Light("192.68.1.35", "80");
            light.OpenOWNConnection();
            light.SetLightStatus("11", "1");
            light.SetLightStatus("25", "1");*/
            sm.Start();
            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
                 time += (float)gameTime.ElapsedGameTime.Milliseconds;
                if (isFirstime && time > 1000f)
                {
                    sender.HIMStartAcquisition();
                    isFirstime = false;
                    time = 0f;
                }
                else if (!isFirstime && time>300f)
                {
                    if (HIM_RESPONSES["LEFT"] == sender.Value())
                    {
                        LeftIsPressed();
                    }
                    if (HIM_RESPONSES["RIGHT"] == sender.Value())
                    {
                        RightIsPressed();

                    }
                    if (HIM_RESPONSES["UP"] == sender.Value())
                    {
                        EnterIsPressed();
                        EnterIsReleased();
                    }
                    if (HIM_RESPONSES["DOWN"] == sender.Value())
                    {
                        XIsPressed();
                    }
                    time = 0f;
                }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //graphics.GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            spriteBatch.Draw(bg, pos[ambientIndex], null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            Color col = (ambienti_label[0].Select) ? Color.Azure : Color.White;
            spriteBatch.Draw(env, new Vector2(380 * screenscale, 350 * screenscale), null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            spriteBatch.DrawString(ambienti_label[0].Font, ambienti_label[0].Text, new Vector2(400 * screenscale, 720 * screenscale), col, 0.0f, new Vector2(0, 0),  screenscale * 1.2f, SpriteEffects.None, 0);
            col = (ambienti_label[1].Select) ? Color.Azure : Color.White;
            spriteBatch.Draw(env, new Vector2(880 * screenscale, 350 * screenscale), null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            spriteBatch.DrawString(ambienti_label[1].Font, ambienti_label[1].Text, new Vector2(900 * screenscale, 720 * screenscale), col, 0.0f, new Vector2(0, 0), screenscale * 1.2f, SpriteEffects.None, 0);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void EnterIsPressed() {
            bg = bg_dark;
        }

        public void EnterIsReleased() {
            bg = bg_light;
            // Return to the main menu.
            if (OnEnvironmentSelect != null) { OnEnvironmentSelect(); }
        }

        public void LeftIsPressed() {
            // Change the selected Ambiente to left.
            ambientIndex = (ambientIndex == 0) ? (ambienti_label.Count - 1) : (ambientIndex - 1);
        }

        public void RightIsPressed()
        {
            // Change the selected Ambiente to right.
            ambientIndex = (ambientIndex == (ambienti_label.Count - 1)) ? 0 : (ambientIndex + 1);
        }

        public void XIsPressed()
        {
           // Return to the main menu.
           if (OnBack != null) { OnBack(); }
        }
    }
}
